//
// Created by gj on 16-2-21.
//

#include "ThreadSharePointer.h"

#include "Thread.h"


// --------------------------------------------- unit test --------------------------------------------------
#ifdef FLAG_UNIT_TESTING

class TestRunnable1 : public CRunnable {

public:
    TestRunnable1() :LOG_INIT_NAME_DEBUG("TestRunnable")
            CRunnable() {
    }

    virtual ~TestRunnable1() {
        LOG_I_FUNC("--> start")
    }

    virtual time_t getRunTime() const {
        return 0;
    }

protected:
    LOG_CREATE_DEBUG

    virtual void *run() {
        LOG_I("--> start run")
        LOG_I("--> end run")
    }
};

class TestRunnable : public CRunnable {
public:
    static AsyncRunnable runnable;

    TestRunnable() :LOG_INIT_NAME_DEBUG("TestRunnable")
            CRunnable() {
    }

    virtual ~TestRunnable() {
        LOG_I_FUNC("--> start")
    }

    virtual time_t getRunTime() const {
        return 0;
    }

protected:
    LOG_CREATE_DEBUG

    virtual void *run() {
        LOG_I("--> start run")
        AsyncRunnable _r = runnable;
        runnable = new TestRunnable1();
        _r = NULL;
        LOG_I("--> end run")
    }
};

AsyncRunnable TestRunnable::runnable(new TestRunnable());

void thread_share_pointer_unit_testing() {
    // TestRunnable::runnable = new AsyncRunnable(new TestRunnable());
    for (int j = 0; j < 10; ++j) {
        for (int i = 0; i < 100; ++i) {
            AsyncRunnable _r(new TestRunnable());
            CThread _thread(_r);
            _thread.detach();
        }
        for (int i = 0; i < 100; ++i) {
            AsyncRunnable _r(new TestRunnable());
            TestRunnable::runnable = _r;
        }
    }
}

#endif